/**
 * @filename:ModelConfigController 2025-04-15
 * @project starhub-schema  V1.0
 */
package com.starhub.engine.model.controller;

import com.starhub.application.interfaces.model.ModelConfigService;
import com.starhub.common.bean.ID;
import com.starhub.common.bean.ResultResp;
import com.starhub.common.bean.model.ModelConfig;
import com.starhub.common.security.util.SecurityUtils;
import com.starhub.common.sys.user.LoginUser;
import com.starhub.utils.Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * <p>说明： 大模型配置API接口层</P>
 * @version: V1.0
 * @author: luosw
 * @time    2025-04-15
 *
 */
@Api(value="大模型配置前台接口", tags = {"大模型配置前台接口"},description = "编写者:luosw" )
@RestController
@RequestMapping("/engine/starhub/model")
public class ModelConfigController {
    @Autowired
    private ModelConfigService modelService;
    /**
    * 日志
    */
    private static final Logger log = LoggerFactory.getLogger(ModelConfigController.class);


    /**
     * 大模型配置新增
     */
    @ApiOperation(value = "大模型配置新增", notes = "大模型配置新增")
    @PostMapping("/add")
    public ResultResp add(@RequestBody ModelConfig model){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            ResultResp = ResultResp.success(modelService.add(user, model).toString());
        }catch (Exception e){
           log.error(ExceptionUtils.getStackTrace(e));
           ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

    /**
     * 大模型配置修改
     */
    @ApiOperation(value = "大模型配置修改", notes = "大模型配置修改")
    @PostMapping("/update")
    public ResultResp update(@RequestBody ModelConfig model){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            ResultResp = ResultResp.success(modelService.update(user, model));
        }catch (Exception e){
           log.error(ExceptionUtils.getStackTrace(e));
           ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

    /**
     * 大模型配置信息获取
     */
    @ApiOperation(value = "大模型配置信息获取", notes = "大模型配置信息获取")
    @GetMapping("/getInfo/{id}")
    public ResultResp getInfo(@PathVariable Long id){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            ResultResp = ResultResp.success(modelService.getInfo(user, id));
        }catch (Exception e){
           log.error(ExceptionUtils.getStackTrace(e));
           ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

    /**
     * 大模型配置逻辑删除
     */
    @ApiOperation(value = "大模型配置逻辑删除", notes = "大模型配置逻辑删除")
    @PostMapping("/delete")
    public ResultResp deleteByType(@RequestBody ID id){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            modelService.deleteByType(user, id.getIds(),1);
            ResultResp = ResultResp.success("删除成功");
        }catch (Exception e){
           log.error(ExceptionUtils.getStackTrace(e));
           ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }


    /**
     * 大模型配置物理删除
     */
    @ApiOperation(value = "大模型配置物理删除", notes = "大模型配置物理删除")
    @PostMapping("/deleteRecord")
    public ResultResp deleteRecord(@RequestBody ID id){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            modelService.deleteRecord(user, id.getIds());
            ResultResp = ResultResp.success("删除成功");
        }catch (Exception e){
           log.error(ExceptionUtils.getStackTrace(e));
           ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }

    
    /**
     * 大模型配置列表获取
     */
    @ApiOperation(value = "大模型配置列表获取", notes = "大模型配置列表获取")
    @GetMapping("/getList")
    public ResultResp getList(HttpServletRequest request){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            Map<String , Object> params = Util.request2Map(request);
            ResultResp = ResultResp.success(modelService.getList(user, params));
        }catch (Exception e){
           log.error(ExceptionUtils.getStackTrace(e));
           ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }


    @ApiOperation(value = "菜单列表获取", notes = "菜单列表获取")
    @GetMapping("/getActiveModel")
    public ResultResp getActiveModel(HttpServletRequest request){
        ResultResp ResultResp = null;
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            Map<String , Object> params = Util.request2Map(request);
            ResultResp = ResultResp.success(modelService.getActiveModel(user, params));
        }catch (Exception e){
            log.error(ExceptionUtils.getStackTrace(e));
            ResultResp = ResultResp.fail(e.getMessage(),e);
        }
        return ResultResp;
    }


}